const express = require("express");
const pool = require("../pool");
const router = express.Router();
module.exports = router;

router.get('/list', (req, res) => {
    const dataPageSize = req.query.dataPageSize;
    const page = req.query.page;
    console.log(`页数：${page}每页：${dataPageSize}`);
    pool.query("select lid,title,price from xz_laptop limit ?,?", [parseInt(page) * parseInt(dataPageSize), parseInt(dataPageSize)], (e, result) => {
        if (e) throw e;
        console.log(...result);
        res.send(result);
    })
})
router.delete('/L_delete', (req, res) => {
    const value = req.query.D_lid;
    console.log(value);
    pool.query("delete from xz_laptop where lid =?", [value], (e, result) => {
        if (e) throw e;
        if (result.affectedRows == 1) {
            res.send('deleted');
            console.log('deleted');
        } else {
            res.send('false');
            console.log('false');
        }
    })
})

router.use(express.urlencoded({extended: false}));
router.post('/add', (req, res) => {
    const title = req.body.Title;
    const price = req.body.price;
    console.log(title, price);
    pool.query("select * from xz_laptop where title=?&&price=?", [title, price], (e, result) => {
        if (e) throw e;
        console.log(result);
        if (result.length !== 0) {
            res.send({
                status: 400,
                data: "Goods_exist"
            });
        } else {
            pool.query("insert into xz_laptop (title,price) values(?,?)", [title, price], (e, result) => {
                if (e) throw e;
                if (result.affectedRows !== 0) {
                    res.send({
                        status: 200,
                        data: "Ok!"
                    })
                }
            })
        }

    })
});